package com.zmm.weblog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmm.weblog.admin.model.vo.user.FindUserInfoRspVo;
import com.zmm.weblog.admin.model.vo.user.UpdateAdminUserPasswordReqVo;
import com.zmm.weblog.admin.service.AdminUserService;
import com.zmm.weblog.common.domain.mapper.UserMapper;
import com.zmm.weblog.common.enums.ResponseCodeEnum;
import com.zmm.weblog.common.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author:福禄寿
 * @Date:2024/4/30 上午11:05
 * @Des:weblog-springboot
 **/
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public Response updatePassword(UpdateAdminUserPasswordReqVo updateAdminUserPasswordReqVo) {
        //获取用户名和密码
        String username = updateAdminUserPasswordReqVo.getUsername();
        String password = updateAdminUserPasswordReqVo.getPassword();

        //密码加密
        String encode = passwordEncoder.encode(password);

        //存入数据库
        int count = userMapper.updatePasswordByUsername(username, encode);

        //返回结果
        return count == 1 ? Response.success() : Response.fail(ResponseCodeEnum.USERNAME_NOT_FOUND);
    }

    @Override
    public Response findUserInfo() {
        //获取用户名
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return Response.success(FindUserInfoRspVo.builder().username(username).build());

    }
}
